package com.example.blogspring.controller;

import com.example.blogspring.common.HttpResult;
import com.example.blogspring.constant.Constants;
import com.example.blogspring.model.UserInfo;
import com.example.blogspring.service.UserService;
import com.example.blogspring.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/22 3:29
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/reg")
    public HttpResult register(UserInfo userInfo){
        if (userInfo==null || !StringUtils.hasLength(userInfo.getUserName())
        ||!StringUtils.hasLength(userInfo.getPassword())){
            return HttpResult.fail(-1,"参数无效");
        }
        return HttpResult.success(userService.add(userInfo));
    }

    @RequestMapping("/login")
    public HttpResult login(UserInfo userInfo, HttpServletRequest request){
        log.info(userInfo.getUserName()+":"+userInfo.getPassword());
        if (userInfo==null || !StringUtils.hasLength(userInfo.getUserName())
        ||!StringUtils.hasLength(userInfo.getPassword())){
            return HttpResult.fail(-2,"用户名或密码为空");
        }
        String username = userInfo.getUserName();
        String password =  userInfo.getPassword();
        UserInfo user = userService.getUserByName(username);
        log.info(user.toString());
        if (user==null || !SecurityUtils.decrypt(password,user.getPassword()) ){
            return HttpResult.fail(-3,"用户名或密码错误");
        }
//        存入session
        HttpSession session = request.getSession(true);
        user.setPassword("");
        session.setAttribute(Constants.SESSION_USER_INFO,user);
        return HttpResult.success(200,"登陆成功",true);

    }
    @RequestMapping("/logout")
    public Boolean logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        UserInfo user = null;
        if (session!=null){
            user= (UserInfo) session.getAttribute(Constants.SESSION_USER_INFO);
            session.removeAttribute(Constants.SESSION_USER_INFO);
            return true;
        }
        log.error("用户退出失败：Session-User : "+user);
        return false;
    }

    /**
     * 博客详情页 获取作者信息
     * @param id
     * @return
     */
    @RequestMapping("/getauthorinfo")
    public UserInfo getAuthorInfo(Integer id){
        if (id==null || id <0){
            return null;
        }
        return userService.getUserInfoByBlogId(id);
    }
    @RequestMapping("/getlogininfo")
    public UserInfo getLoginInfo(HttpServletRequest request){
        return (UserInfo) request.getSession(false).getAttribute(Constants.SESSION_USER_INFO);
    }
}
